package com.example.fish.service.impl;

import com.example.fish.mapper.IsRacedMapper;
import com.example.fish.mapper.RaceMapper;
import com.example.fish.mapper.UserInfoMapper;
import com.example.fish.pojo.*;
import com.example.fish.service.RaceService;
import com.example.fish.service.ex.WorkInsertException;
import com.example.fish.utils.StaticVariable;
import com.example.fish.vo.Top3Info;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RaceServiceImpl implements RaceService {
    @Autowired
    private RaceMapper raceMapper;
    @Autowired
    private IsRacedMapper isRacedMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void addRace(Integer userid, String summary, MultipartFile file) {
//        封装race对象
        Race race = new Race();
        race.setUserid(userid);
        race.setSummary(summary);
//        得到时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        race.setTime(currentDate);
//        处理图片
        String fileName = StaticVariable.updateFile(file, StaticVariable.WORKIMG_PATH);
        race.setImg(fileName);
//        保存数据
        int insert = raceMapper.insert(race);
        if (insert == 0){
            throw new WorkInsertException("插入失败");
        }
    }

    /***
     * 分页查询所有的可以参与评选的race
     * 0 --->最新报名的，下次参与的
     * 1---->正在评选的，下次展示的
     * 2---->正在展示的，
     * >2--->可以删除的
     */
    @Override
    public PageInfo<Race> getRaceList(Integer pageNum) {
//        查询所有，根据canrace判断参赛资格，排序之后返回数据
        Integer pageSize = 6;
        RaceExample example = new RaceExample();
        example.setOrderByClause("score DESC");
        example.createCriteria().andCanraceEqualTo(1);
        int i = raceMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
//        查询全部为待评选状态的race
        List<Race> races = raceMapper.selectByExample(example);
        PageInfo<Race> pageInfo = new PageInfo<>(races);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        return pageInfo;
    }

    /***
     * 获取canrace  == 0
     */
    @Override
    public PageInfo<Race> getNextRaceList(Integer pageNum) {
        Integer pageSize = 6;
        RaceExample example = new RaceExample();
        example.createCriteria().andCanraceEqualTo(0);
        int i = raceMapper.countByExample(example);
        PageHelper.startPage(pageNum,pageSize);
        List<Race> races = raceMapper.selectByExample(example);
        PageInfo<Race> pageInfo = new PageInfo<>(races);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setTotal(i);
        return pageInfo;
    }

    /***
     * 投票    ---->需要判断
     * 先通过userid查询 israce 表，通过raceid且canrace == 1
     * 1. 判断用户是否已经投票   2.投票的canrace是否是1
     */
    @Override
    public Boolean select(Integer userid, Integer raceid) {
        Boolean flag = false ;
//        返回是否投过票
        IsRacedExample isRacedExample = new IsRacedExample();
        isRacedExample.createCriteria().andUseridEqualTo(userid);
        List<IsRaced> isRaceds = isRacedMapper.selectByExample(isRacedExample);
        for (IsRaced isRaced : isRaceds) {
//            通过raceid查询
            Race race = raceMapper.selectByPrimaryKey(isRaced.getRaceid());
            if (race.getCanrace() == 1){
//                投过票
                flag = true;
                break;
            }
        }
        return flag;
    }

    /***
     * 投票
     * score +1   //   is race存数据
     */
    @Override
    public void vote(Integer userid, Integer raceid) {
        Race race = raceMapper.selectByPrimaryKey(raceid);
        race.setScore(race.getScore()+1);
        raceMapper.updateByPrimaryKey(race);
        IsRaced isRaced = new IsRaced();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());
        isRaced.setRaceid(raceid);
        isRaced.setUserid(userid);
        isRaced.setTime(currentDate);
        isRacedMapper.insert(isRaced);
    }

    /***
     * 获得此次参与投票的总票数
     * 查询所有canrace为1 的score和
     */
    @Override
    public Integer getVoteCount() {
        Integer count = 0;
        RaceExample raceExample = new RaceExample();
        raceExample.createCriteria().andCanraceEqualTo(1);
        List<Race> races = raceMapper.selectByExample(raceExample);
        for (Race race : races) {
            count += race.getScore();
        }
        return count;
    }

    /***
     * 选出canrace == 2 的前三名
     */
    @Override
    public List<Top3Info> getTop3() {
        RaceExample example = new RaceExample();
        example.setOrderByClause("score DESC");
        example.createCriteria().andCanraceEqualTo(2);
        List<Race> races = raceMapper.selectByExample(example);
//        选出races前三名
        List<Race> races1 = races.subList(0, 3);
        List<Top3Info> list = new ArrayList<>();
//        封装
        for (Race race : races1) {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(race.getUserid());
            Top3Info top3Info = new Top3Info();
            top3Info.setUserid(userInfo.getUserid());
            top3Info.setAvatar(userInfo.getAvator());
            top3Info.setUsername(userInfo.getName());
            top3Info.setRaceid(race.getRaceid());
            top3Info.setRaceImg(race.getImg());
            top3Info.setScore(race.getScore());
            list.add(top3Info);
        }
        return list;
    }
}
